package com.edu.controller;

import com.edu.bean.Book;
import com.edu.bean.User;
import com.edu.service.BookService;
import com.edu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;


@Controller
public class BookController {
    @Resource
    private BookService bookService;

    @ModelAttribute
    public void init(Model model){
        model.addAttribute("book",new Book());
    }

    @RequestMapping("/bookDelete")
    public ModelAndView bookDelete(int bookId){
        bookService.deleteBookById(bookId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("bookList",bookService.getAllBook());
        mv.setViewName("listBook");
        return mv;
    }

    @RequestMapping("/bookUpdate")
    public ModelAndView bookUpdate(Book book){
        bookService.updateBookById(book);
        ModelAndView mv = new ModelAndView();
        mv.addObject("bookList",bookService.getAllBook());
        mv.setViewName("listBook");
        return mv;
    }

//    /**
//     * 简单将book给分页且展示
//     * @param page 第几页
//     * @param pageSize 每页长度
//     * @return
//     */
//    @RequestMapping("/listBook")
//    public ModelAndView listBook(@RequestParam(value = "page", defaultValue = "1") int page,@RequestParam(value = "pageSize", defaultValue = "5") int pageSize
//                                  ) {
//        ModelAndView mv = new ModelAndView();
//        int bookCount = bookService.getBookCount();
//        int pageCount = (int) Math.ceil((double) bookCount / pageSize);
//        mv.addObject("bookList", bookService.getBookByPage(page, pageSize));
//        mv.addObject("currentPage", page);//第几页
//        mv.addObject("pageCount", pageCount);//页数
//        mv.addObject("pageSize", pageSize);//每页条数
//        mv.addObject("pageNumbers", IntStream.rangeClosed(1, pageCount).boxed().collect(Collectors.toList()));//把页数打包成集合方便遍历
//        mv.setViewName("books.jsp");
//        return mv;
//    }

    @Resource
    HttpSession session;
    /**
     * 可以进行多条件查询并分页
     */
    @RequestMapping(value="/listBook" )
    public ModelAndView listBook(@RequestParam(value = "page", defaultValue = "0") int page,
                                 @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                                 @ModelAttribute("book") Book book
    ) {
        ModelAndView mv = new ModelAndView();
        if (book.getBookName()==null&&book.getBookWriter()==null&&book.getBookName()==null&&book.getPutDate()==null){
            book = (Book) session.getAttribute("bookSession");
            if (book==null){
                book=new Book();
            }
        }
        System.out.println(book.getPutDate());
        session.setAttribute("bookSession",book);
        int bookCount = bookService.getBookCountByBook(book);
        int pageCount = (int) Math.ceil((double) bookCount / pageSize);
        mv.addObject("bookList", bookService.getBookByPageAndBook(page, pageSize,book));
        mv.addObject("currentPage", page);//第几页
        mv.addObject("pageCount", pageCount);//页数
        mv.addObject("pageSize", pageSize);//每页条数
        mv.addObject("pageNumbers", IntStream.rangeClosed(1, pageCount).boxed().collect(Collectors.toList()));//把页数打包成集合方便遍历
        mv.setViewName("books.jsp");
        return mv;
    }
}
